<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<style type="text/css">
		body, html {width: 100%;height: 100%;margin:0;font-family:"微软雅黑";font-family:"微软雅黑";}
		#allmap{width:100%;height:500px;}
		p{margin-left:5px; font-size:14px;}
	</style>
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=EX8DyYmj9Mmm14kjdz1wYk7vYO0CCTDy"></script>
	
	<script type="text/javascript" src="../company/jquery-1.10.1.js"></script>
	<title>地图单击事件</title>
</head>
<body>
	<input type="hidden" id ="address" value="">
	<input type="hidden" id ="lat" value="">
	<input type="hidden" id ="lin" value="">
	<div style="height: 50px;line-height: 50px;text-align: center;">
    <div style="margin-left:50px;float: left">
        <input type="text" id="suggestId" size="20" value="百度" style="width:200px;margin-bottom: 10px;"
               placeholder="请输入地址检索"/>
        <a href="javascript:void(0);" onclick="document.getElementById('suggestId').value=''">清除</a>
    </div>
    点击地图任意一点获取坐标
</div>
<!--<div id="r-result">请输入地址检索：</div>-->
<div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
<!--用于即时搜索提示-->
<div id="allmap"></div><!--用于放置地图-->
</body>
</html>
<script>
    //var map = new BMap.Map("allmap");
    //设置中心点坐标
    var point = new BMap.Point(114.325629, 30.557636);
 
    //初始化地图
    /**
     *地图类型：
     *普通地图：BMAP_NORMAL_MAP
     *卫星地图：BMAP_HYBRID_MAP
     */
    var map = new BMap.Map("allmap", {mapType: BMAP_NORMAL_MAP, minZoom: 1, maxZoom: 18});
    //设置地图类型及最小最大级别
 
    var marker = new BMap.Marker(point); //将点转化成标注点
    map.addOverlay(marker);  //将标注点添加到地图上
 
    //设置地图级别（1-18）
    map.centerAndZoom(point, 12);
    //开启滚轮缩放地图
    map.enableScrollWheelZoom();
 
    function G(id) {
        return document.getElementById(id);
    }
 
    var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
        {
            "input": "suggestId"
            , "location": map
        });
 
    ac.addEventListener("onhighlight", function (e) {  //鼠标放在下拉列表上的事件
        var str = "";
        var _value = e.fromitem.value;
        var value = "";
        if (e.fromitem.index > -1) {
            value = _value.province + _value.city + _value.district + _value.street + _value.business;
        }
        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
 
        value = "";
        if (e.toitem.index > -1) {
            _value = e.toitem.value;
            value = _value.province + _value.city + _value.district + _value.street + _value.business;
        }
        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
        G("searchResultPanel").innerHTML = str;
    });
 
    var myValue;
    ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
        var _value = e.item.value;
        myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
        G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
 
        setPlace();
    });
 
    var geoc = new BMap.Geocoder(); 
    map.addEventListener("click", function(e){   
        //通过点击百度地图，可以获取到对应的point, 由point的lng、lat属性就可以获取对应的经度纬度     
        var pt = e.point;
        geoc.getLocation(pt, function(rs){
            //addressComponents对象可以获取到详细的地址信息
            var addComp = rs.addressComponents;
            var site = addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber;
            //alert(site);
            //alert(pt.lng);
            //alert(pt.lat);
            //将对应的HTML元素设置值
            //$("#site").val(site);
            //$("#longitude").val(pt.lng);
            //$("#latitude").val(pt.lat);
        });        
    });
    
    function getAddr(){
    	var obj={};
    	
    	var address =$("#address").val();
    	var lat =$("#lat").val();
    	var lin =$("#lin").val();
    	obj.address=address;
    	obj.lat=lat;
    	obj.lin=lin;
    	return obj;
    }
    
    function setPlace() {
    	//sgetAddr();
        map.clearOverlays();    //清除地图上所有覆盖物
        function myFun() {
            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
            map.centerAndZoom(pp, 18);
            map.addOverlay(new BMap.Marker(pp));    //添加标注
            $("#address").val(myValue);
            //纬度
            $("#lat").val(pp.lat);
            //经度
            $("#lin").val(pp.lng);
            //alert(pp.lng);
            //alert(pp.lat);
        }
 
        var local = new BMap.LocalSearch(map, { //智能搜索
            onSearchComplete: myFun
        });
        
        alert(myValue);
        local.search(myValue);
    }
</script>
